import { Application, Container, Sprite, Texture } from 'pixi.js';

export class RenderEngine {
  private app: Application;
  private mainContainer: Container;
  private gameObjects: Map<string, Sprite>;

  constructor(width: number, height: number) {
    // 创建PIXI应用实例
    this.app = new Application({
      width,
      height,
      backgroundColor: 0x000000,
      antialias: false,
      resolution: window.devicePixelRatio || 1,
    });

    // 创建主容器
    this.mainContainer = new Container();
    this.app.stage.addChild(this.mainContainer);

    // 初始化游戏对象存储
    this.gameObjects = new Map();

    // 设置像素风格
    this.app.renderer.resolution = 1;
    this.app.stage.scale.set(1); // 不进行缩放，保持原始大小
  }

  // 获取渲染视图
  public getView(): HTMLCanvasElement {
    return this.app.view as HTMLCanvasElement;
  }

  // 调整渲染器大小
  public resize(width: number, height: number): void {
    this.app.renderer.resize(width, height);
  }

  // 添加游戏对象
  public addGameObject(id: string, texture: Texture): Sprite {
    const sprite = new Sprite(texture);
    this.gameObjects.set(id, sprite);
    this.mainContainer.addChild(sprite);
    return sprite;
  }

  // 移除游戏对象
  public removeGameObject(id: string): void {
    const sprite = this.gameObjects.get(id);
    if (sprite) {
      if (sprite.parent) {
        this.mainContainer.removeChild(sprite);
      }
      this.gameObjects.delete(id);
      if (!sprite.destroyed) {
        sprite.destroy();
      }
    }
  }

  // 更新游戏对象位置
  public updateGameObjectPosition(id: string, x: number, y: number): void {
    const sprite = this.gameObjects.get(id);
    if (sprite) {
      sprite.x = x;
      sprite.y = y;
    }
  }

  // 更新摄像机位置
  public updateCamera(targetX: number, targetY: number): void {
    // 计算摄像机位置（使目标位置居中）
    const screenWidth = this.app.screen.width;
    const screenHeight = this.app.screen.height;
    
    // 计算主容器的新位置（反向移动以实现摄像机跟随）
    const newX = screenWidth / 2 - targetX;
    const newY = screenHeight / 2 - targetY;
    
    // 更新主容器位置
    this.mainContainer.position.set(newX, newY);
  }

  // 更新游戏对象旋转
  public updateGameObjectRotation(id: string, rotation: number): void {
    const sprite = this.gameObjects.get(id);
    if (sprite) {
      sprite.rotation = rotation;
    }
  }

  // 添加动态光效
  public addLightEffect(x: number, y: number, color: number, intensity: number): Sprite {
    const light = new Sprite(Texture.WHITE);
    light.tint = color;
    light.alpha = intensity;
    light.position.set(x, y);
    light.blendMode = 1; // 添加混合模式
    this.mainContainer.addChild(light);
    return light;
  }

  // 清理资源
  public destroy(): void {
    this.gameObjects.clear();
    this.app.destroy(true);
  }

  // 获取渲染器实例
  public getRenderer(): Application {
    return this.app;
  }

  // 开始游戏循环
  public startGameLoop(updateCallback: (delta: number) => void): void {
    this.app.ticker.add(updateCallback);
  }

  // 停止游戏循环
  public stopGameLoop(updateCallback: (delta: number) => void): void {
    this.app.ticker.remove(updateCallback);
  }
}